/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ScriptWindow.java
 *
 * Created on 26.10.2009, 21:03:55
 */
package trim.gui.testing;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import trim.gui.testing.components.JFontChooserDialog;
import trim.gui.testing.export.ExportManager;
import trim.gui.testing.script.ImportData;

/**
 *
 * @author Дмитрий
 */
public class ScriptWindow extends javax.swing.JDialog
{

  Map<String,String> templates=new HashMap();
  HighLightThread highLightThread;
  boolean isScriptSaved=false;
  File scriptFile=null;

  /** Creates new form ScriptWindow */
  public ScriptWindow(java.awt.Frame parent,boolean modal)
  {
    super(parent,modal);
    initComponents();
    highLightThread=new HighLightThread();
    try
    {
      BufferedReader r=new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(
        "/trim/gui/testing/ScriptTemplates.properties")));
      String s;
      int i=0;
      while((s=r.readLine())!=null)
      {
        int e=s.indexOf("=");
        if(e<0)
          continue;
        String name=s.substring(0,e);
        String value=s.substring(e+1).replace("\\n","\n");
        templates.put(name,value);
        JButton b=new JButton();
        b.addActionListener(new TemplateActionListener(templates,scriptText));
        b.setText(name);
        pTemplateButtons.add(b);
        i++;
      }
      ((GridLayout)pTemplateButtons.getLayout()).setRows(i);
      pTemplateButtons.setPreferredSize(new Dimension(140,15*i));
    }catch(IOException ex)
    {
      ex.printStackTrace();
    }
    highLightThread.start();
  }

  /** This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    buttonPanel = new javax.swing.JPanel();
    btOpenScript = new javax.swing.JButton();
    btRunScript = new javax.swing.JButton();
    btClose = new javax.swing.JButton();
    btSaveScript = new javax.swing.JButton();
    scriptSplitPane = new javax.swing.JSplitPane();
    spTemplateButtons = new javax.swing.JScrollPane();
    pTemplateButtons = new javax.swing.JPanel();
    jScrollPane2 = new javax.swing.JScrollPane();
    scriptText = new javax.swing.JTextPane();

    addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyPressed(java.awt.event.KeyEvent evt) {
        formKeyPressed(evt);
      }
    });

    btOpenScript.setText("Open script");
    btOpenScript.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btOpenScriptActionPerformed(evt);
      }
    });

    btRunScript.setText("Run script (F5)");
    btRunScript.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btRunScriptActionPerformed(evt);
      }
    });

    btClose.setText("Close");
    btClose.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btCloseActionPerformed(evt);
      }
    });

    btSaveScript.setText("Save script (Ctrl+S)");
    btSaveScript.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        btSaveScriptActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout buttonPanelLayout = new javax.swing.GroupLayout(buttonPanel);
    buttonPanel.setLayout(buttonPanelLayout);
    buttonPanelLayout.setHorizontalGroup(
      buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(buttonPanelLayout.createSequentialGroup()
        .addComponent(btOpenScript)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(btSaveScript)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(btRunScript)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 436, Short.MAX_VALUE)
        .addComponent(btClose))
    );
    buttonPanelLayout.setVerticalGroup(
      buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
        .addComponent(btOpenScript)
        .addComponent(btClose)
        .addComponent(btSaveScript)
        .addComponent(btRunScript))
    );

    scriptSplitPane.setDividerLocation(150);

    spTemplateButtons.setPreferredSize(new java.awt.Dimension(110, 100));

    pTemplateButtons.setMaximumSize(new java.awt.Dimension(150, 32767));
    pTemplateButtons.setPreferredSize(new java.awt.Dimension(110, 400));
    pTemplateButtons.setLayout(new java.awt.GridLayout(10, 1));
    spTemplateButtons.setViewportView(pTemplateButtons);

    scriptSplitPane.setLeftComponent(spTemplateButtons);

    scriptText.setFont(new java.awt.Font("Courier New", 0, 13)); // NOI18N
    scriptText.addInputMethodListener(new java.awt.event.InputMethodListener() {
      public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
      }
      public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
        scriptTextInputMethodTextChanged(evt);
      }
    });
    scriptText.addKeyListener(new java.awt.event.KeyAdapter() {
      public void keyPressed(java.awt.event.KeyEvent evt) {
        scriptTextKeyPressed(evt);
      }
      public void keyTyped(java.awt.event.KeyEvent evt) {
        scriptTextKeyTyped(evt);
      }
    });
    jScrollPane2.setViewportView(scriptText);

    scriptSplitPane.setRightComponent(jScrollPane2);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addComponent(buttonPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addComponent(scriptSplitPane, javax.swing.GroupLayout.DEFAULT_SIZE, 824, Short.MAX_VALUE))
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addComponent(buttonPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addContainerGap(321, Short.MAX_VALUE))
      .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
          .addGap(28, 28, 28)
          .addComponent(scriptSplitPane, javax.swing.GroupLayout.DEFAULT_SIZE, 316, Short.MAX_VALUE)))
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

    private void btRunScriptActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btRunScriptActionPerformed
    {//GEN-HEADEREND:event_btRunScriptActionPerformed

      ScriptStatusWindow ssw=new ScriptStatusWindow(this,scriptText.getText(),
        new ExportManager(),
        new ImportData(((FmMain)getParent()).getMainInputData()),
        ((FmMain)getParent()).getResultListTableModel(),
        AppState.getPlaginManager());

    }//GEN-LAST:event_btRunScriptActionPerformed

    private void btOpenScriptActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btOpenScriptActionPerformed
    {//GEN-HEADEREND:event_btOpenScriptActionPerformed
      try
      {
        JFileChooser fileChooser=new JFileChooser(AppState.CURRENT_FILE);
        int returnVal=fileChooser.showOpenDialog(this);
        if(returnVal==JFileChooser.APPROVE_OPTION)
        {
          File f=fileChooser.getSelectedFile();
          scriptFile=f;
          isScriptSaved=true;
          this.setTitle(f.getName()+" - tester");
          AppState.CURRENT_FILE=f.getParentFile();
          InputStream is;
          is=new FileInputStream(fileChooser.getSelectedFile());
          ByteArrayOutputStream os=new ByteArrayOutputStream(1024);
          byte[] buff=new byte[1024];
          int c;
          while((c=is.read(buff))>0)
            os.write(buff,0,c);
          is.close();
          os.close();
          scriptText.setText(new String(os.toByteArray(),"utf-8"));
          highLightThread.resume();
        }
      }catch(Exception ex)
      {
        ex.printStackTrace();
      }
    }//GEN-LAST:event_btOpenScriptActionPerformed

    private void btCloseActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btCloseActionPerformed
    {//GEN-HEADEREND:event_btCloseActionPerformed
      setVisible(false);
    }//GEN-LAST:event_btCloseActionPerformed

    private void formKeyPressed(java.awt.event.KeyEvent evt)//GEN-FIRST:event_formKeyPressed
    {//GEN-HEADEREND:event_formKeyPressed
      if(evt.getKeyCode()==KeyEvent.VK_F5)
        btRunScriptActionPerformed(null);
    }//GEN-LAST:event_formKeyPressed

    private void btSaveScriptActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btSaveScriptActionPerformed
    {//GEN-HEADEREND:event_btSaveScriptActionPerformed
      JFileChooser fileChooser=new JFileChooser(AppState.CURRENT_FILE);
      int r=fileChooser.showSaveDialog(this);
      if(r==JFileChooser.APPROVE_OPTION)
      {
        try
        {
          File f=fileChooser.getSelectedFile();
          AppState.CURRENT_FILE=f.getParentFile();
//          Writer wr=new FileWriter(f);
//          wr.write(scriptText.getText());
//          wr.close();
          OutputStream os=new FileOutputStream(f);
          os.write(scriptText.getText().getBytes("UTF-8"));
          os.close();
          isScriptSaved=true;
          scriptFile=f;
          setTitle(scriptFile.getName());
        }catch(Exception ex)
        {
          ex.printStackTrace();
        }
      }
    }//GEN-LAST:event_btSaveScriptActionPerformed

    private void scriptTextKeyPressed(java.awt.event.KeyEvent evt)//GEN-FIRST:event_scriptTextKeyPressed
    {//GEN-HEADEREND:event_scriptTextKeyPressed
      try
      {
        if(evt.isControlDown()&&evt.getKeyCode()==KeyEvent.VK_S)
        {
          OutputStream os;
          try
          {
            if(!isScriptSaved)
              btSaveScriptActionPerformed(null);
            else
            {
              os=new FileOutputStream(scriptFile);
              os.write(scriptText.getText().getBytes("UTF-8"));
              os.close();
            }
            setTitle(scriptFile.getName());
          }catch(Exception ex)
          {
            Logger.getLogger(ScriptWindow.class.getName()).log(Level.SEVERE,null,
              ex);
          }

          return;
        }
        if(evt.getKeyCode()==KeyEvent.VK_F5)
        {
          btRunScriptActionPerformed(null);
          return;
        }
        if(evt.getKeyCode()!=KeyEvent.VK_TAB&&evt.getKeyCode()!=KeyEvent.VK_ENTER)
          return;
        if(scriptText.getCaretPosition()==0)
          return;
        int cp=scriptText.getCaretPosition()-1;
        String txt=scriptText.getDocument().getText(0,
          scriptText.getDocument().getLength());
        int s=cp;
        switch(evt.getKeyCode())
        {
          case 9:
            if(txt.charAt(cp)=='\n')
            {
              scriptText.getDocument().insertString(scriptText.getCaretPosition(),
                "    ",null);
            }else
            {
              while(s>=0&&txt.charAt(s)!=' '&&txt.charAt(s)!='\n'&&txt.charAt(s)!=';'&&txt.charAt(
                s)!='.'&&txt.charAt(s)!='}'&&txt.charAt(s)!='{'&&txt.charAt(s)!='('&&txt.charAt(
                s)!=')'&&txt.charAt(s)!='['&&txt.charAt(s)!=']'&&txt.charAt(s)!='=')
                s--;
              String word=txt.substring(s+1,cp+1);
              if(templates.get(word)==null)
              {
                scriptText.getDocument().insertString(scriptText.getCaretPosition(),
                  "    ",null);
              }else
              {
                //scriptText.replaceRange(templates.get(word),s+1,cp+1);
                scriptText.select(s+1,cp+1);
                scriptText.replaceSelection(templates.get(word));
              }
            }
            break;
          case (int)'\n':
            while(s>=0&&txt.charAt(s)!='\n')
              s--;
            int n=0;
            s++;
            while(s<txt.length()&&txt.charAt(s)==' ')
            {
              s++;
              n++;
            }
            scriptText.getDocument().insertString(scriptText.getCaretPosition(),
              "\n"+"                                              ".substring(0,n),null);
            break;
        }
        evt.setKeyCode(0);
      }catch(BadLocationException ex)
      {
        Logger.getLogger(ScriptWindow.class.getName()).log(Level.SEVERE,null,ex);
      }
    }//GEN-LAST:event_scriptTextKeyPressed

    private void scriptTextKeyTyped(java.awt.event.KeyEvent evt)//GEN-FIRST:event_scriptTextKeyTyped
    {//GEN-HEADEREND:event_scriptTextKeyTyped
      if(!evt.isControlDown())
        setTitle(scriptFile!=null?scriptFile.getName()+"*":"*");
//      if(scriptText.getCaretPosition()==0)
//        return;
//      int cp=scriptText.getCaretPosition()-1;
//      String txt=scriptText.getText();
//      //if(txt.charAt(cp)=='\n')
//      //  return;
//      int s=cp;
//      while(s>=0&&txt.charAt(s)!=' '&&txt.charAt(s)!='\n'&&txt.charAt(s)!=';'&&txt.charAt(s)!='.')
//        s--;
//      int e=cp;
//      while(e<txt.length()&&txt.charAt(e)!=' '&&txt.charAt(e)!='\n'&&txt.charAt(e)!=';'&&txt.charAt(e)!='.')
//        e++;
//
//      if(s==e)
//        return;
//      String word=txt.substring(s+1,e);
//      if("qwe".equals(word))
//        scriptText.getStyledDocument().setCharacterAttributes(s+1,e,scriptText.getStyle("Bold"),true);
//      else
//        scriptText.getStyledDocument().setCharacterAttributes(s,e,scriptText.getStyle("Text"),true);
      highLightThread.resume();

    }//GEN-LAST:event_scriptTextKeyTyped

    private void scriptTextInputMethodTextChanged(java.awt.event.InputMethodEvent evt)//GEN-FIRST:event_scriptTextInputMethodTextChanged
    {//GEN-HEADEREND:event_scriptTextInputMethodTextChanged
    }//GEN-LAST:event_scriptTextInputMethodTextChanged
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton btClose;
  private javax.swing.JButton btOpenScript;
  private javax.swing.JButton btRunScript;
  private javax.swing.JButton btSaveScript;
  private javax.swing.JPanel buttonPanel;
  private javax.swing.JScrollPane jScrollPane2;
  private javax.swing.JPanel pTemplateButtons;
  private javax.swing.JSplitPane scriptSplitPane;
  private javax.swing.JTextPane scriptText;
  private javax.swing.JScrollPane spTemplateButtons;
  // End of variables declaration//GEN-END:variables

  private class TemplateActionListener implements ActionListener
  {

    Map<String,String> templates;
    JTextPane scriptText;

    public TemplateActionListener(Map<String,String> templates,
      JTextPane scriptText)
    {
      this.templates=templates;
      this.scriptText=scriptText;
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
      writeScriptText(templates.get(((JButton)e.getSource()).getText()));
    }

    private void writeScriptText(String text)
    {
      try
      {
        scriptText.getDocument().insertString(scriptText.getCaretPosition(),text,
          null);
      }catch(BadLocationException ex)
      {
        ex.printStackTrace();
      }
      scriptText.grabFocus();
      highLightThread.resume();
    }
  }

  private class HighLightThread extends Thread
  {

    private Map<String,String> dict=new HashMap();
    // private StringReader scriptReader;
    private Style keyWordStyle, textStyle, stringStyle, commentStyle;
    private int ch;
    private Lexem currenLexem=new Lexem();
//переменные для посимвольного чтения
    private int index;
    private String text;
    private int markIndex;

    public HighLightThread()
    {
      try
      {
        Style style=scriptText.addStyle("KeyWord",null);
        StyleConstants.setBold(style,true);
        StyleConstants.setForeground(style,Color.BLUE);
        keyWordStyle=style;
        style=scriptText.addStyle("Text",null);
        textStyle=style;
        style=scriptText.addStyle("String",null);
        StyleConstants.setForeground(style,new Color(206,123,0));
        stringStyle=style;
        style=scriptText.addStyle("Comment",null);
        StyleConstants.setBold(style,true);
        StyleConstants.setForeground(style,new Color(0,102,51));
        commentStyle=style;
        BufferedReader r=new BufferedReader(new InputStreamReader(getClass().getResourceAsStream(
          "/trim/gui/testing/highlight.dict")));
        String s;
        int i=0;
        while((s=r.readLine())!=null)
        {
          dict.put(s,s);
        }

      }catch(IOException ex)
      {
        ex.printStackTrace();
      }
    }

    @Override
    public void run()
    {
      super.run();
      while(true)
      {
        try
        {
          sleep(50);
          StringBuilder sb;

          markIndex=index=s=e=0;

          String txt=scriptText.getDocument().getText(0,
            scriptText.getDocument().getLength());
          text=txt;
          scriptText.setIgnoreRepaint(true);
          int s, e;
          s=e=0;
          Lexem l;
          HighlightState state=HighlightState.TEXT;
          while((l=getNextLexem())!=null)
          {
            //System.out.println(txt.substring(l.start,l.end));
            switch(l.type)
            {
              case APOS:
                markText(s,e,textStyle);
                s=l.start;
                e=l.end;
                while((l=getNextLexem())!=null&&l.type!=LexemType.APOS&&l.type!=LexemType.NEW_LINE)
                  e=l.end;
                if(l!=null)e=l.end;
                markText(s,e,stringStyle);
                s=e;
                break;
              case QUOT:
                markText(s,e,textStyle);
                s=l.start;
                e=l.end;
                while((l=getNextLexem())!=null&&l.type!=LexemType.QUOT&&l.type!=LexemType.NEW_LINE)
                  e=l.end;
                if(l!=null)e=l.end;
                markText(s,e,stringStyle);
                s=e;
                break;
              case OPEN_COMMENT:
                markText(s,e,textStyle);
                s=l.start;
                e=l.end;
                while((l=getNextLexem())!=null&&l.type!=LexemType.CLOSE_COMMENT)
                  e=l.end;
                if(l!=null)e=l.end;
                markText(s,e,commentStyle);
                s=e;
                break;
              case LINE_COMMENT:
                markText(s,e,textStyle);
                s=l.start;
                e=l.end;
                while((l=getNextLexem())!=null&&l.type!=LexemType.NEW_LINE)
                  e=l.end;
                if(l!=null)e=l.end;
                markText(s,e,commentStyle);
                s=e;
                break;
              case DELIMETER:
                e=l.end;
                break;
              case WORD:
                String word=text.substring(l.start,l.end);
                if(dict.containsKey(word))
                {
                  markText(s,e,textStyle);
                  markText(l.start,l.end,keyWordStyle);
                  s=e=l.end;
                }else{
                  e=l.end;
                }
                break;
            }
          }
          markText(s,e,textStyle);
          suspend();
        }catch(Exception ex)
        {
          ex.printStackTrace();
          suspend();
        }
        scriptText.setIgnoreRepaint(false);
      }
    }
    int s=0, e=0;

    private Lexem getNextLexem() throws IOException
    {
      ch=read();
      if(ch>=0)
      {
        char c=(char)ch;
        switch(c)
        {
          case ' ':
          case ';':
          case '.':
          case '}':
          case '{':
          case '(':
          case ')':
          case '[':
          case ']':
          case ',':
          case '=':
          case '-':
          case '+':
            e++;
            currenLexem.type=LexemType.DELIMETER;
            currenLexem.start=s;
            currenLexem.end=e;
            s=e;
            return currenLexem;
          case '\n':
            e++;
            currenLexem.type=LexemType.NEW_LINE;
            currenLexem.start=s;
            currenLexem.end=e;
            s=e;
            return currenLexem;
          case '\'':
            e++;
            currenLexem.type=LexemType.APOS;
            currenLexem.start=s;
            currenLexem.end=e;
            s=e;
            return currenLexem;
          case '"':
            e++;
            currenLexem.type=LexemType.QUOT;
            currenLexem.start=s;
            currenLexem.end=e;
            s=e;
            return currenLexem;
          case '*':
            mark(0);
            ch=read();
            if(ch<0||((char)ch!='/'))
            {
              reset();
              e++;
              currenLexem.type=LexemType.DELIMETER;
              currenLexem.start=s;
              currenLexem.end=e;
              s=e;
              return currenLexem;
            }else
            {
              e+=2;
              currenLexem.type=LexemType.CLOSE_COMMENT;
              currenLexem.start=s;
              currenLexem.end=e;
              s=e;
              return currenLexem;
            }
          case '/':
            mark(0);
            ch=read();
            if(ch<0||((char)ch!='/'&&(char)ch!='*'))
            {
              reset();
              e++;
              currenLexem.type=LexemType.DELIMETER;
              currenLexem.start=s;
              currenLexem.end=e;
              s=e;
              return currenLexem;
            }else
            {
              e+=2;
              if(((char)ch=='/'))
                currenLexem.type=LexemType.LINE_COMMENT;
              else
                currenLexem.type=LexemType.OPEN_COMMENT;
              currenLexem.start=s;
              currenLexem.end=e;
              s=e;
              return currenLexem;
            }
          default:
            //scriptReader.skip(s);
            while((ch=read())>=0)
            {
              c=(char)ch;
              if(c!=' '&&c!=';'&&c!='.'&&c!='}'&&c!='{'&&c!='('&&c!=')'&&c!='['&&c!=']'&&c!=','&&c!=
                '='&&c!='-'&&c!='+'&&c!='\''&&c!='"'&&c!='\n'&&c!='*'&&c!='/')
                e++;
              else
                break;
            }
            if(ch>=0)
            {
              skip(-1);
            }
            e++;
            currenLexem.type=LexemType.WORD;
            currenLexem.start=s;
            currenLexem.end=e;
            s=e;
            return currenLexem;

        }
      }else
        return null;
    }

    private int read()
    {
      if(index<text.length())
      {
        index++;
        return (int)text.charAt(index-1);
      }else
        return -1;
    }

    private void skip(int n)
    {
      index+=n;
    }

    private void mark(int x)
    {
      markIndex=index;
    }

    private void reset()
    {
      index=markIndex;
    }

    private void markText(int s,int e,Style style)
    {
      scriptText.getStyledDocument().setCharacterAttributes(s,e-s,
        style,true);
    }
//    private int getCharacter(int e) throws IOException
//    {
//      while((ch=scriptReader.read())>=0&&((char)ch!='\'')&&((char)ch!='\n'))
//        e++;
//      return e;
//    }
//
//    private int getString(int e) throws IOException
//    {
//      while((ch=scriptReader.read())>=0&&((char)ch!='\"')&&((char)ch!='\n'))
//        e++;
//      return e;
//    }
  }

  private class Lexem
  {

    public LexemType type;
    public int start;
    public int end;
  }

  enum LexemType
  {

    DELIMETER, APOS, QUOT, WORD, NEW_LINE, OPEN_COMMENT, CLOSE_COMMENT, LINE_COMMENT
  }

  enum HighlightState
  {

    TEXT, KEY_WORD, STRING, COMMENT, LINE_COMMENT
  }
}
